/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DesenvolvedorView.java
 *
 * Created on 28/10/2011, 04:31:06
 */
package br.com.geftu.view;

import br.com.geftu.HibernateUtil.HibernateUtil;
import br.com.geftu.util.ProtegerSenha;
import br.com.geftu.dao.DAO;
import br.com.geftu.model.Desenvolvedor;
import br.com.geftu.model.Participa;
import br.com.geftu.util.GerarSenha;
import br.com.geftu.util.MaskF;
import br.com.geftu.util.ValidaCPF;
import br.com.geftu.util.ValidaInsert;
import com.towel.el.annotation.AnnotationResolver;
import com.towel.swing.table.ObjectTableModel;
import java.awt.HeadlessException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Session;
import javax.swing.JFormattedTextField;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.hibernate.exception.ConstraintViolationException;

/**
 *
 * @author jorgenei
 */
public final class DesenvolvedorView extends javax.swing.JFrame {

    LoginView login = new LoginView();
    List<Desenvolvedor> lista_desenvolvedor = new ArrayList();
    List<Participa> lista_participa = new ArrayList();
    Integer posicao_atual = 0;
    String ordem = "order by iddes";
    String listar = "from Desenvolvedor ";

    /**
     * Creates new form DesenvolvedorView
     */
    public DesenvolvedorView() {
        initComponents();
        preencherJtableDesenvolvedor();
        bloqueiaCampos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        groupordenar = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        tf_pesquisa = new javax.swing.JTextField();
        lb_pesquisa = new javax.swing.JLabel();
        bt_new = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        bt_excluir = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        rb_nome = new javax.swing.JRadioButton();
        rb_codigo = new javax.swing.JRadioButton();
        jButton5 = new javax.swing.JButton();
        bt_senha = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tf_login = new javax.swing.JTextField();
        lb_senha = new javax.swing.JLabel();
        tf_nome = new javax.swing.JTextField();
        tf_codigo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tf_senha = new javax.swing.JTextField();
        tf_cpf = tf_cpf = new JFormattedTextField(MaskF.mascara("cpf"));
        lb_perfil = new javax.swing.JLabel();
        cb_perfil = new javax.swing.JComboBox();
        tf_fone = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        lb_desenvolvedor = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cadastro Desenvolvedores");

        jPanel1.setName("jPanel1"); // NOI18N

        tf_pesquisa.setName("tf_pesquisa"); // NOI18N
        tf_pesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_pesquisaKeyPressed(evt);
            }
        });

        lb_pesquisa.setText("Pesquisa:");
        lb_pesquisa.setName("lb_pesquisa"); // NOI18N

        bt_new.setText("Novo");
        bt_new.setName("bt_new"); // NOI18N
        bt_new.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_newActionPerformed(evt);
            }
        });

        jButton2.setText("Salvar");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        bt_excluir.setText("Excluir");
        bt_excluir.setName("bt_excluir"); // NOI18N
        bt_excluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_excluirActionPerformed(evt);
            }
        });

        jPanel4.setBackground(new java.awt.Color(204, 255, 153));
        jPanel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel4.setName("jPanel4"); // NOI18N

        jLabel8.setText("Ordenar:");
        jLabel8.setName("jLabel8"); // NOI18N

        rb_nome.setBackground(new java.awt.Color(204, 255, 153));
        groupordenar.add(rb_nome);
        rb_nome.setText("Nome");
        rb_nome.setName("rb_nome"); // NOI18N
        rb_nome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_nomeActionPerformed(evt);
            }
        });

        rb_codigo.setBackground(new java.awt.Color(204, 255, 153));
        groupordenar.add(rb_codigo);
        rb_codigo.setSelected(true);
        rb_codigo.setText("Código");
        rb_codigo.setName("rb_codigo"); // NOI18N
        rb_codigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_codigoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rb_nome)
                    .addComponent(rb_codigo))
                .addContainerGap(48, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rb_codigo)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rb_nome)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton5.setText("Fechar");
        jButton5.setName("jButton5"); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        bt_senha.setText("Gerar Senha");
        bt_senha.setName("bt_senha"); // NOI18N
        bt_senha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_senhaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(lb_pesquisa)
                        .addGap(18, 18, 18)
                        .addComponent(tf_pesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(bt_new)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_excluir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_senha)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(143, 143, 143))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lb_pesquisa)
                            .addComponent(tf_pesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(bt_new)
                            .addComponent(jButton2)
                            .addComponent(bt_excluir)
                            .addComponent(jButton5)
                            .addComponent(bt_senha)))
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setName("jPanel3"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "ID", "Nome", "CPF", "Login", "Perfil", "Phone"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(10);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(70);
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(40);
        jTable1.getColumnModel().getColumn(3).setPreferredWidth(40);
        jTable1.getColumnModel().getColumn(4).setPreferredWidth(40);
        jTable1.getColumnModel().getColumn(5).setMinWidth(0);
        jTable1.getColumnModel().getColumn(5).setMaxWidth(0);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel2.setName("jPanel2"); // NOI18N

        jLabel4.setText("CPF:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel1.setText("Código:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel3.setText("Nome:");
        jLabel3.setName("jLabel3"); // NOI18N

        tf_login.setName("tf_login"); // NOI18N
        tf_login.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tf_loginFocusLost(evt);
            }
        });
        tf_login.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                tf_loginInputMethodTextChanged(evt);
            }
        });
        tf_login.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                tf_loginPropertyChange(evt);
            }
        });

        lb_senha.setText("Senha:");
        lb_senha.setName("lb_senha"); // NOI18N

        tf_nome.setName("tf_nome"); // NOI18N
        tf_nome.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tf_nomeFocusLost(evt);
            }
        });

        tf_codigo.setName("tf_codigo"); // NOI18N

        jLabel5.setText("Login:");
        jLabel5.setName("jLabel5"); // NOI18N

        tf_senha.setName("tf_senha"); // NOI18N
        tf_senha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_senhaActionPerformed(evt);
            }
        });

        tf_cpf.setName("tf_cpf"); // NOI18N
        tf_cpf.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tf_cpfFocusLost(evt);
            }
        });

        lb_perfil.setText("Perfil:");
        lb_perfil.setName("lb_perfil"); // NOI18N

        cb_perfil.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Administrador", "Usuário" }));
        cb_perfil.setName("cb_perfil"); // NOI18N

        tf_fone.setName("tf_fone"); // NOI18N
        tf_fone.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tf_foneFocusLost(evt);
            }
        });
        tf_fone.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                tf_foneInputMethodTextChanged(evt);
            }
        });
        tf_fone.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                tf_fonePropertyChange(evt);
            }
        });

        jLabel6.setText("Fone:");
        jLabel6.setName("jLabel6"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(tf_codigo, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(28, 28, 28)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tf_nome))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(tf_cpf, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(tf_login, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(lb_senha)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(tf_senha, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(19, 19, 19)
                                .addComponent(tf_fone, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(lb_perfil, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(9, 9, 9)
                                .addComponent(cb_perfil, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tf_codigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)
                        .addComponent(tf_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tf_login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_senha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lb_senha)
                    .addComponent(jLabel5)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(tf_cpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(tf_fone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lb_perfil)
                    .addComponent(cb_perfil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        lb_desenvolvedor.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lb_desenvolvedor.setText("Lista de desenvolvedores");
        lb_desenvolvedor.setName("lb_desenvolvedor"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lb_desenvolvedor, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 560, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addComponent(lb_desenvolvedor)
                .addGap(5, 5, 5)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 555, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setSize(new java.awt.Dimension(592, 443));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (LoginView.d.getPerfil().equals("Administrador")) {

            try {

                if (tf_codigo.getText().equals("")) {
                    Desenvolvedor desenvolvedor = new Desenvolvedor();
                    desenvolvedor.setNome(tf_nome.getText().toString());
                    desenvolvedor.setCpf(tf_cpf.getText().toString().replaceAll("[.-]", ""));
                    desenvolvedor.setLogin(tf_login.getText().toString());
                    desenvolvedor.setCelular(tf_fone.getText().toString());
                    desenvolvedor.setPerfil(cb_perfil.getSelectedItem().toString());
                    //Classe Proteger senha
                    ProtegerSenha senha = new ProtegerSenha();
                    desenvolvedor.setSenha(senha.geraHash(tf_senha.getText()));
                    JOptionPane.showMessageDialog(null, desenvolvedor.getSenha());

                    if (ValidaInsert.isvalid(desenvolvedor).isEmpty()) {
                        DAO objdesenvolvedor = new DAO(desenvolvedor);
                        objdesenvolvedor.insere();
                        preencherJtableDesenvolvedor();
                    } else {
                        JOptionPane.showMessageDialog(null, ValidaInsert.isvalid(desenvolvedor));
                        // tf_nome.requestFocus();
                    }
                } else {

                    String alterar = "Tem certeza de que deseja alterar o registro?";
                    int opcao_escolhida = JOptionPane.showConfirmDialog(null, alterar, "Exclusão ", JOptionPane.YES_OPTION);
                    if (opcao_escolhida == JOptionPane.YES_OPTION) {
                        try {
                            Session sessao = HibernateUtil.getSessionFactory().openSession();
                            Desenvolvedor temp = (Desenvolvedor) sessao.load(Desenvolvedor.class, Integer.parseInt(tf_codigo.getText()));

                            Desenvolvedor desenvolvedor = new Desenvolvedor();
                            JOptionPane.showMessageDialog(null, "compara: " + desenvolvedor.equals(temp));
                            desenvolvedor.setIddes(Integer.parseInt(tf_codigo.getText()));
                            desenvolvedor.setNome(tf_nome.getText().toString());
                            desenvolvedor.setCpf(tf_cpf.getText().toString().replaceAll("[.-]", ""));
                            desenvolvedor.setLogin(tf_login.getText().toString());
                            desenvolvedor.setCelular(tf_fone.getText().toString());
                            desenvolvedor.setPerfil(cb_perfil.getSelectedItem().toString());
                            desenvolvedor.setSenha(temp.getSenha());

                            JOptionPane.showMessageDialog(null, "getnome: " + desenvolvedor.getNome());
                            JOptionPane.showMessageDialog(null, "getiddes: " + desenvolvedor.getIddes());
                            if (ValidaInsert.isvalid(desenvolvedor).isEmpty()) {
                                DAO objdesenvolvedor = new DAO(desenvolvedor);
                                //     JOptionPane.showMessageDialog(null, objdesenvolvedor.lista_um(listar, ordem, alterar));
                                objdesenvolvedor.insere();
                                preencherJtableDesenvolvedor();
                            } else {
                                JOptionPane.showMessageDialog(null, ValidaInsert.isvalid(desenvolvedor));
                            }

                        } catch (NumberFormatException | HibernateException e) {
                            JOptionPane.showMessageDialog(null, e);
                        }

                    }

                }

            } catch (HeadlessException | NumberFormatException | ConstraintViolationException erro) {
                JOptionPane.showMessageDialog(null, "Erro na insersão : " + erro);
            }
        } else {
            String alterar = "Tem certeza de que deseja alterar seu cadastro?";
            int opcao_escolhida = JOptionPane.showConfirmDialog(null, alterar, "Exclusão ", JOptionPane.YES_OPTION);
            if (opcao_escolhida == JOptionPane.YES_OPTION) {
                try {

                    Session sessao = HibernateUtil.getSessionFactory().openSession();
                    Desenvolvedor desenvolvedor = LoginView.d;
                    desenvolvedor.setNome(tf_nome.getText().toString());
                    desenvolvedor.setCpf(tf_cpf.getText().toString().replaceAll("[.-]", ""));
                    desenvolvedor.setCelular(tf_fone.getText().toString());

                    if (ValidaInsert.isvalid(desenvolvedor).isEmpty()) {
                        DAO objdesenvolvedor = new DAO(desenvolvedor);
                        objdesenvolvedor.insere();
                        preencherJtableDesenvolvedor();
                    } else {
                        JOptionPane.showMessageDialog(null, ValidaInsert.isvalid(desenvolvedor));
                    }

                } catch (NumberFormatException | HibernateException e) {
                    JOptionPane.showMessageDialog(null, e);
                }
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void tf_senhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_senhaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_senhaActionPerformed

    private void bt_newActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_newActionPerformed
        desbloqueiaCampos();
        tf_codigo.setText("");
        tf_nome.setText("");
        tf_cpf.setText("");
        tf_login.setText("");
        tf_senha.setText("");
        tf_fone.setText("");
        cb_perfil.setSelectedItem("Usuário");
        tf_nome.requestFocus();

    }//GEN-LAST:event_bt_newActionPerformed

    private void rb_nomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rb_nomeActionPerformed
        ordem = "order by nome";
        preencherJtableDesenvolvedor();
    }//GEN-LAST:event_rb_nomeActionPerformed

    private void rb_codigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rb_codigoActionPerformed

        ordem = "order by iddes";
        preencherJtableDesenvolvedor();
    }//GEN-LAST:event_rb_codigoActionPerformed

    private void bt_excluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_excluirActionPerformed
        String excluir = "Tem certeza de que deseja excluir o registro " + tf_nome.getText() + "?";

        int opcao_escolhida = JOptionPane.showConfirmDialog(null, excluir, "Exclusão ", JOptionPane.YES_OPTION);
        if (opcao_escolhida == JOptionPane.YES_OPTION) {
            try {
                Desenvolvedor desenvolvedor = new Desenvolvedor();
                desenvolvedor.setIddes(Integer.parseInt(tf_codigo.getText()));
                DAO obdesenv = new DAO(desenvolvedor);
                obdesenv.excluir();
                preencherJtableDesenvolvedor();

            } catch (NumberFormatException | HeadlessException erro) {
                JOptionPane.showMessageDialog(null, "Erro na insersão : " + erro);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Registro não excluído.");
        }
    }
        // TODO add your handling code here:        // TODO add your handling code here:}//GEN-LAST:event_bt_excluirActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        encerrar();         // TODO add your handling code here:}//GEN-LAST:event_jButton5ActionPerformed
    }
        private void tf_pesquisaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_pesquisaKeyPressed
            listar = "from Desenvolvedor where upper(nome) like '%" + tf_pesquisa.getText().toUpperCase() + "%'";
            preencherJtableDesenvolvedor();
            // TODO add your handling code here:
    }//GEN-LAST:event_tf_pesquisaKeyPressed

    private void tf_cpfFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tf_cpfFocusLost

        try {
            Boolean situac = ValidaCPF.validaCPF(tf_cpf.getText().toString());
            JOptionPane.showMessageDialog(null, situac);

            if (tf_cpf.getText().length() == 11) {
                JOptionPane.showMessageDialog(null, "cpfok");
            }
            if (!situac) {
                String aviso = "O CPF informado é inválido, deseja corrigir?";
                int opcao_escolhida = JOptionPane.showConfirmDialog(null, aviso, "Exclusão ", JOptionPane.YES_OPTION);
                if (opcao_escolhida == JOptionPane.YES_OPTION) {
                    tf_cpf.requestFocus();
                }
            } else {
                Desenvolvedor desenvolvedor = new Desenvolvedor();
                DAO verificaCPF = new DAO(desenvolvedor);
                List result;
                result = verificaCPF.lista_campo_existe("Desenvolvedor.findByCpf", "cpf", tf_cpf.getText().replaceAll("[.-]", ""));
                if (result.size() > 0) {
                    JOptionPane.showMessageDialog(null, "CPF já cadastrado");
                } else {

                }
            }

        } catch (HeadlessException e) {
            JOptionPane.showMessageDialog(null, e.getStackTrace());
        }


    }//GEN-LAST:event_tf_cpfFocusLost

    private void tf_loginFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tf_loginFocusLost
        // TODO add your handling code here:
        Desenvolvedor desenvolvedor = new Desenvolvedor();
        DAO verificaLogin = new DAO(desenvolvedor);
        List result;
        result = verificaLogin.lista_campo_existe("Desenvolvedor.findByLogin", "login", tf_login.getText());
        if (tf_codigo.getText().isEmpty()) {
            try {
                if (result.size() > 0) {
                    JOptionPane.showMessageDialog(null, "Login já em uso");
                    tf_login.setText("");
                    tf_login.requestFocus();
                } else {
                }
            } catch (HeadlessException e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }

    }//GEN-LAST:event_tf_loginFocusLost

    private void bt_senhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_senhaActionPerformed
        // TODO add your handling code here:
        tf_senha.setText(GerarSenha.getRandomPassword());
    }//GEN-LAST:event_bt_senhaActionPerformed

    private void tf_nomeFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tf_nomeFocusLost
        // TODO add your handling code here:

    }//GEN-LAST:event_tf_nomeFocusLost

    private void tf_loginPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_tf_loginPropertyChange
        // TODO add your handling code here:

    }//GEN-LAST:event_tf_loginPropertyChange

    private void tf_loginInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_tf_loginInputMethodTextChanged
        // TODO add your handling code here:

    }//GEN-LAST:event_tf_loginInputMethodTextChanged

    private void tf_foneFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tf_foneFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_foneFocusLost

    private void tf_foneInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_tf_foneInputMethodTextChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_foneInputMethodTextChanged

    private void tf_fonePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_tf_fonePropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_fonePropertyChange

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        try {
            int linha = jTable1.getSelectedRow();
            tf_codigo.setText(jTable1.getValueAt(linha, 0).toString());
            tf_nome.setText(jTable1.getValueAt(linha, 1).toString());
            tf_cpf.setText(jTable1.getValueAt(linha, 2).toString());
            tf_login.setText(jTable1.getValueAt(linha, 3).toString());
            cb_perfil.setSelectedItem(jTable1.getValueAt(linha, 4).toString());
            tf_fone.setText(jTable1.getValueAt(linha, 5).toString());
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, e);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }


    }//GEN-LAST:event_jTable1MouseClicked

    /**
     * @param args the command line arguments
     */
    //xx1
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new DesenvolvedorView().setVisible(true);
            }
        });
    }//xx1
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_excluir;
    private javax.swing.JButton bt_new;
    private javax.swing.JButton bt_senha;
    private javax.swing.JComboBox cb_perfil;
    private javax.swing.ButtonGroup groupordenar;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lb_desenvolvedor;
    private javax.swing.JLabel lb_perfil;
    private javax.swing.JLabel lb_pesquisa;
    private javax.swing.JLabel lb_senha;
    private javax.swing.JRadioButton rb_codigo;
    private javax.swing.JRadioButton rb_nome;
    private javax.swing.JTextField tf_codigo;
    private javax.swing.JFormattedTextField tf_cpf;
    private javax.swing.JTextField tf_fone;
    private javax.swing.JTextField tf_login;
    private javax.swing.JTextField tf_nome;
    private javax.swing.JTextField tf_pesquisa;
    private javax.swing.JTextField tf_senha;
    // End of variables declaration//GEN-END:variables

    public void preencherJtableDesenvolvedor() {

        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        modelo.setNumRows(0);

        if (LoginView.d.getPerfil().equals("Administrador")) {
            try {
                Session sessao = HibernateUtil.getSessionFactory().openSession();

                lista_desenvolvedor = sessao.createQuery(listar + ordem).list();

                int tamanho_lista = lista_desenvolvedor.size();

                for (int i = 0; i < tamanho_lista; i++) {
                    Desenvolvedor desenvolvedor = lista_desenvolvedor.get(i);
                    modelo.addRow(new Object[]{desenvolvedor.getIddes(), desenvolvedor.getNome(), desenvolvedor.getCpf(), desenvolvedor.getLogin(), desenvolvedor.getPerfil(), desenvolvedor.getCelular()});
                }
                if (!lista_desenvolvedor.isEmpty()) {
                    Desenvolvedor desenvolvedor = lista_desenvolvedor.get(0);
                    tf_codigo.setText("" + desenvolvedor.getIddes());
                    tf_nome.setText(desenvolvedor.getNome());
                    tf_cpf.setText(desenvolvedor.getCpf());
                    tf_login.setText(desenvolvedor.getLogin());
                    cb_perfil.setSelectedItem(desenvolvedor.getPerfil());
                    tf_codigo.setEnabled(false);
                    lista_desenvolvedor = sessao.createQuery(listar + ordem).list();
                    tf_fone.setText(desenvolvedor.getCelular());
                    //bt_verifica.setVisible(false);                
                }

            } catch (Exception erro) {
                JOptionPane.showMessageDialog(null, "Erro na insersão : " + erro);
            }

        } else {
            tf_codigo.setText("" + LoginView.d.getIddes());
            tf_nome.setText(LoginView.d.getNome());
            tf_cpf.setText(LoginView.d.getCpf());
            tf_login.setText(LoginView.d.getLogin());
            tf_fone.setText(LoginView.d.getCelular());
            tf_codigo.setEnabled(false);
            lb_desenvolvedor.setVisible(false);
            jScrollPane1.setVisible(false);


        }

    }

    public void bloqueiaCampos() {
        if (LoginView.d.getPerfil().equals("Administrador")) {
            tf_login.setEditable(false);
            lb_senha.setVisible(false);
            tf_senha.setVisible(false);
            bt_senha.setVisible(false);
        } else {
            tf_login.setEditable(false);
            lb_senha.setVisible(false);
            tf_senha.setVisible(false);
            bt_senha.setVisible(false);
            cb_perfil.setVisible(false);
            lb_perfil.setVisible(false);
            tf_pesquisa.setVisible(false);
            jTable1.setVisible(false);
            jPanel4.setVisible(false);
            lb_pesquisa.setVisible(false);
            bt_new.setVisible(false);
            bt_excluir.setVisible(false);

        }
    }

    public void desbloqueiaCampos() {
        if (LoginView.d.getPerfil().equals("Administrador")) {
            tf_login.setEditable(true);
            lb_senha.setVisible(true);
            tf_senha.setVisible(true);
            bt_senha.setVisible(true);
        } else {
            tf_login.setEditable(true);
        }

    }

    public void encerrar() {
        setVisible(false);
        RelatoView.getInstance().setVisible(true);
    }
}
